import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { RoleService } from '../../role/role.service';

@Injectable()
export class RoleGuard implements CanActivate {
  constructor(
    private readonly reflector: Reflector,
    private readonly roleService: RoleService,
  ) {}
  async canActivate(context: ExecutionContext): Promise<boolean> {
    const roleDct = this.reflector.getAllAndOverride<string[]>('roleDct', [
      context.getHandler(),
      context.getClass(),
    ]);
    // console.log('roleDct', roleDct);
    const request = context.switchToHttp().getRequest();
    const { url } = request;
    console.log('url', url);
    if (!roleDct) {
      return true;
    }

    const { roles } = request.user;
    return roles.some((role) => roleDct.includes(role));
  }
}
